home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.clark.net!bms88!stuart
- From: stuart@bmsi.com (Stuart D. Gathman)
- Subject: Re: SORTing problem c++
- Organization: Business Management Systems, Inc., Fairfax, VA
- Date: Thu, 1 Feb 1996 22:27:53 GMT
- Message-ID: <1996Feb1.222753.20805@bmsi.com>
- References: <310B0DD0.34F1@pi.net>
-
- In article <310B0DD0.34F1@pi.net> you write:
- >I'm used to normal c
- >just recent i switched to c++, now i am stucked
- >with a program that did work in c but doesn't
- >in c++.
- >it is a program that sorts the 'argv'command line
- >arguments array,
- >
- >what can I DO???
- >p.s. I am using borlands c++
- >p.s. thanks for reading this
- >
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <string.h>
- >
- >
- >int sort_function(char **a, char **b)
- >{
- > return( strcmp(*a, *b));
- >}
- >
- >
- >int main(int argc, char **argv)
- >{
- > int
- > x;
- >
- > qsort(argv, argc, sizeof(char*), sort_function);
- >
- > for (x = 0; x < argc; x++)
- > printf("%s\n", argv[x]);
- > return 0;
- >}
- >
- >******************************************
- >these are the error mesages,
- >
- >
- >Error ..\SOURCES\OPDR_45.CPP 17: Cannot convert 'int (*)(char * *,char *
- >*)' to 'int (*)(const void *,const void *)'
- >Error ..\SOURCES\OPDR_45.CPP 17: Type mismatch in parameter '__fcmp' in
- >call to 'qsort(void *,unsigned int,unsigned int,int (*)(const void
- >*,const void *))'
-
- You program was not portable even in C. The C++ compiler is trying
- to tell you that your sort_function is the wrong type to pass
- to qsort(). qsort wants a function with the signature specified in
- the first error message:
-
- int sort_function(const void *a, const void *b) {
- const char **ap = (const char **)a;
- const char **bp = (const char **)b;
- return( strcmp(*ap, *bp));
- }
-
- Yes, this is ugly. This is why C++ supports polymorphism and would
- not use qsort except inside a class wrapper.
-
- BTW, as written by you, your program would only run on byte addressable
- machines where the hardware format of a void * happens to be the
- same as a const char **.
- --
- Stuart D. Gathman <stuart@bmsi.com> / <..!uunet!bms88!stuart>
- Business Management Systems Inc.
- Phone: 703 591-0911 Fax: 703 591-6154
- "Microsoft is the QWERTY of Operating Systems" - SDG
-